﻿@using System.Diagnostics.CodeAnalysis
@using WinFormBlazorDesigner.Ext
@using WinformBlazor.Components
<div id="@Id" class="drop-area @(IsContainer())" style="z-index:@(Control.Style.z_index);@(Control.Style.ToString());">    </div>


@code
{
    public string Id { get; set; } = Guid.NewGuid().ToString();
    [Inject]
    [NotNull]
    public DesignerJsInterop? JS { get; set; }
    [CascadingParameter]
    public Page? MainPage { get; set; }
    [Parameter]
    [NotNull]
    public ControlInfo? Control { get; set; }
    [Parameter]
    public ControlInfo? Parent { get; set; }
    public string IsContainer()
    {
        switch (Control.Type)
        {
            case ContrloType.Form:
                break;
            case ContrloType.Panel:
                return "container";
            case ContrloType.Textbox:
                break;
            case ContrloType.Button:
                break;

            case ContrloType.Label:
                break;
            case ContrloType.CheckBox:
                break;
            default:
                break;
        }
        return " ";
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            await JS.adddrag(Id);
        }
    }
    void ondragend(DragEventArgs e)
    {
        ;
    }
    /// <summary>
    /// 拖拽结束:
    /// 1.获取到当前鼠标的坐标p1
    /// 2.找到父级容器的大小,父级容器的坐标p2
    /// 3.p1-p2等于此控件相对于父级容器的坐标,将此坐标改为百分比
    /// 4.添加到childs集合
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    async Task ondrop(DragEventArgs e)
    {
        var localp = await JS.getPositionByContainer(Control.Id, e.ClientX, e.ClientY);
        if (MainPage != null)
        {
            var con = MainPage.SelectedBox.GetControl();
            if (con != null)
            {
                con.Style.z_index = ++MainPage.Index;
                if (Control.Type == ContrloType.Panel || Control.Type == ContrloType.Form)
                {

                    Control.Childs.Add(con);
                }
                else
                {
                    //找到这个控件的父级
                    var p = MainPage.MainPage.FindParentById(Control.Name);
                    if (p != null)
                    {
                        p.Childs.Add(con);
                    }
                    else
                    { }
                }
                MainPage?.UpdateHander?.Invoke();
            }
        }



    }
}